
package com.dps.omc.service.impl;
import com.dps.common.utils.tree.TreeBuilder;
import com.dps.common.utils.tree.TreeNode;
import com.dps.omc.dao.ITemplateTypeDAO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.dps.common.service.impl.AbstractPageService;
import com.dps.omc.model.TemplateType;
import com.dps.omc.service.ITemplateTypeService;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@SuppressWarnings("unchecked")
@Service("TemplateTypeServiceImpl")

public class TemplateTypeServiceImpl extends AbstractPageService<ITemplateTypeDAO,TemplateType> implements ITemplateTypeService {

	@Autowired
	private ITemplateTypeDAO templateTypeDAO;
	@Override
	public boolean getEnableDataPerm() {
		// TODO Auto-generated method stub
		return false;
	}
	@Override
	public ITemplateTypeDAO getDao() {
		return templateTypeDAO;
	}

	@Override
	public TreeNode getTypeTree() {
		List<TemplateType> types = this.findAll();

		List<TreeNode> treeNodes = new ArrayList<>();
		for(TemplateType type:types)
		{
			TreeNode node = new TreeNode();
			node.setId(type.getId());
			node.setName(type.getTypeName());
			node.setPid(type.getPid());
			treeNodes.add(node);
		}
		TreeNode typeTree = TreeBuilder.buildTree(treeNodes);
		return typeTree;
	}

	@Override
	public String genId(String pid) {

		List<TemplateType> list = this.findList("pid",pid);
		String id = "";
		if(list.size()>0)
		{
			Map<String,Object> conditions = new HashMap<>();
			conditions.put("pid", pid);
			TemplateType max = (TemplateType) this.queryList(conditions, "id", "desc").get(0);
			id = String.valueOf(Integer.parseInt(max.getId())+1);
		}
		else
		{
			Integer pidVal = Integer.parseInt(pid);
			pidVal = pidVal*100+1;
			id = String.valueOf(pidVal);
		}
		return id;
	}
}
